package com.example.lib;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;

import com.google.gson.Gson;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class AsynCollect extends AsyncTask<Void, Void, ArrayList<String>> {

	private ProgressDialog progressDialog;
	private Context context;
	File file;
	private Handler handler;

	public AsynCollect(Context context, File file, Handler handler) {
		super();
		this.handler = handler;
		this.context = context;
		this.file = file;
	}

	@Override
	protected void onPreExecute() {
		progressDialog = new ProgressDialog(context);
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setTitle("Searching Epub File !!! ");
		progressDialog.show();

		super.onPreExecute();
	}

	@Override
	protected ArrayList<String> doInBackground(Void... params) {
		// TODO Auto-generated method stub
		return CollectEpub.walkdir(file);
	}

	@Override
	protected void onPostExecute(ArrayList<String> result) {
		// TODO Auto-generated method stub

		Message message = new Message();
		message.what = 1;
		Gson gson = new Gson();
		String myx = gson.toJson(result);
		Bundle bundle = new Bundle();

		bundle.putString("list", myx);
		message.setData(bundle);

		handler.sendMessage(message);
		progressDialog.dismiss();
		super.onPostExecute(result);
	}

}
